package main.flex.helper
{
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.getQualifiedClassName;
	
	import main.flex.component.main.FlexAnimatedCat;
	import main.flex.conf.MainConf;
	import main.flex.control.MainController;
	import main.flex.control.MediaController;
	import main.flex.manager.section.WiiavieMediaWidgetManager;
	import main.flex.manager.section.WiiavieVisioWidgetManager;
	import main.flex.model.MainModel;
	import main.flex.model.MediaModel;
	import main.flex.value.MediaListTO;
	import main.flex.value.UserListTO;
	import main.flex.view.WiiavieMainView;
	
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.servebox.foundry.model.ModelLocator;
	import org.servebox.foundry.observation.IObservable;
	import org.servebox.foundry.observation.Notification;
	import org.servebox.foundry.view.ViewHelper;


	public class WiiavieMainViewHelper extends ViewHelper
	{
		[Bindable]
		public var visioViewVisibility : Boolean = false;
		
		[Bindable]
		private var espaceId		:		int;
		
		[Bindable]
		private var _wallpaper		:		String;
		
		public var firstTimeVisio	: Boolean = true;
		public var firstTimeMedia	: Boolean = true;
		
		public function WiiavieMainViewHelper()
		{
			super();
		}
		
		// CONNEXION VIEW HELPER		
		public function toLoggIn():void
		{
			var login : String = getCurrentView().connexionView.loggin.text;
			var mdp : String = getCurrentView().connexionView.pwd.text;
			MainController.getInstance().connectUser( login, mdp );
		}
		
		public function hideWallpaperHandler ( ) : void
		{
			getCurrentView().headerTemplate.wallpaperHandler.visible = false;
			getCurrentView().headerTemplate.wallpaperLabel.visible = false;
		}
		
		private function showWallpaperHandler ( ) : void
		{
			getCurrentView().headerTemplate.wallpaperHandler.visible = true;
			getCurrentView().headerTemplate.wallpaperLabel.visible = true;
		}
		
		[Bindable("userLogged")]
		public function get user() : UserListTO
		{
			return getMainModel().user;
		}
		
		[Bindable("mediaListChanged")]
		public function get mediaList() : MediaListTO
		{
			return getMediaModel().mediaList;
		}
		
		public function get wallpaper( ) : String
		{
			return _wallpaper;
		}
		
		public function set wallpaper( val : String ) : void
		{
			//Alert.show( "set wallpaper url to : "+ val);
			switch( getCurrentView().currentState )
			{
				case WiiavieMainView.MEDIA_VIEW_STATE:
					//getCurrentView().mediaView.bgImg.source = val;
					break;
				case WiiavieMainView.VISIO_VIEW_STATE:
					//getCurrentView().visioView.bgImg.source = val;
					break;
				case WiiavieMainView.CALENDAR_VIEW_STATE:
					//getCurrentView().calendarView.bgImg.source = val;
					break;
				default:
					//getCurrentView().connexionView.bgImg.source = val;
					// à changer lorsque l'on aura la page d'accueil, suivant la structure de la vue
					break;
			}
		}
		
		public function userLoggedNotification_Fired( o : IObservable, n :Notification ) : void
		{
			dispatchEvent( new Event("userLogged") );
			//trace("userLoggedNotification received.");
			if(user.user[0].loggedIn == "true"){
				//Alert.show("Bienvenue " + user.user[0].name + " " + user.user[0].surname ,"Bienvenue : ");
				getCurrentView().connexionView.currentState="loggedIn";
				getCurrentView().headerTemplate.myName.text = user.user[0].name;
				getCurrentView().headerTemplate.myName.visible = true;
				FlexAnimatedCat( getCurrentView().helpTemplate.helpHolder.getChildAt( 0 ) ).currentSection = MainConf.HOME_VIEW;
				showWallpaperHandler();
			}else{
				//Alert.show( "Merci de vous identifier..." ,"Identification :");	
				if(user.user[0].userId == "-1"){
					Alert.show( "Votre mot de passe ou votre login est erroné..." ,"Identification : ");
				}	
			}
		}
		
		public function backToHome(): void
		{
			getCurrentView().currentState="";
			FlexAnimatedCat( getCurrentView().helpTemplate.helpHolder.getChildAt( 0 ) ).currentSection = MainConf.HOME_VIEW;
		}
		
		public function goToGame(): void
		{
			var url : URLRequest = new URLRequest("http://192.168.1.2/jeux/index.htm");
			try{
				navigateToURL( url, "_blank");
			}catch (e:Error){
				Alert.show('test' + e);
			}
		}
		
		public function initVisio():void
		{
			if(user.user[0].userId!=0 && user.user[0].userId!="0" && user.user[0].userId!="-1" && user.user[0].userId!=-1 ){
				if(firstTimeVisio){	
					firstTimeVisio = false;
					espaceId = user.user[0].espaceId;
					getCurrentView().currentState="visioViewState";
					initDatas();
					//initMediaDatas();
				}else{
					getCurrentView().currentState="visioViewState";
				}
				FlexAnimatedCat( getCurrentView().helpTemplate.helpHolder.getChildAt( 0 ) ).currentSection = MainConf.VISIO_VIEW;
			}
		}
		// FIN CONNEXION VIEW HELPER
		
		// MEDIA VIEW HELPER
		[Bindable("userLogged")]
		public function getEspaceId():int
		{
			return espaceId;
		}
		
		public function mediaListChangedNotification_Fired( o : IObservable, n :Notification ) : void
		{
			dispatchEvent( new Event("mediaListChanged") );
		}
		
		protected function getMediaController ( ) : MediaController
		{
			return MainController.getInstance().getSubController( getQualifiedClassName( MediaController ) ) as MediaController;
		}
		
		// FIN MEDIA VIEW HELPER
		
		// VISIO VIEW HELPER
		public function initDatas():void
		{
			getCurrentView().visioView.getWiiavieVisioWidgetManager().setCurrentUser=user.user;
			getCurrentView().visioView.getWiiavieVisioWidgetManager().init();
		}
		
		public function initMedia():void
		{
			if(firstTimeMedia){
				//la rubrique media se lance la première fois
				firstTimeMedia = false;
				espaceId = user.user[0].espaceId;
				getMediaController().getMedia( espaceId );
				getCurrentView().currentState="mediaViewState";
				getCurrentView().mediaView.getWiiavieMediaWidgetManager().mediaList=mediaList;
				getMediaWidgetManager().espaceId = espaceId;
			}else{
				getCurrentView().currentState="mediaViewState";
			}
			FlexAnimatedCat( getCurrentView().helpTemplate.helpHolder.getChildAt( 0 ) ).currentSection = MainConf.MEDIA_VIEW;
		}
		
		public function initCalendar() :void
		{
			getCurrentView().currentState="calendarViewState";
			FlexAnimatedCat( getCurrentView().helpTemplate.helpHolder.getChildAt( 0 ) ).currentSection = MainConf.CALENDAR_VIEW;
		}
		
		public function refreshMedia():void
		{
				firstTimeMedia = false;
				espaceId = user.user[0].espaceId;
				getMediaController().getMedia( espaceId );
				getCurrentView().currentState="mediaViewState";
				getCurrentView().mediaView.getWiiavieMediaWidgetManager().mediaList=mediaList;
				getMediaWidgetManager().espaceId = espaceId;
		}
		
		// FIN VISIO VIEW HELPER
		/* implémentation du model */
		override public function registerToModels() : void
		{
			ModelLocator.getInstance().getModel( MainController.MAIN_MODEL_ID ).registerObserver( this );
			ModelLocator.getInstance().getModel( MediaController.MEDIA_MODEL_ID ).registerObserver( this );
		}
		
		// Shortcuts
		/**
		* return a reference to MainModel
		*/
		
		protected function getMainModel() : MainModel
		{
			return MainModel ( ModelLocator.getInstance().getModel( MainController.MAIN_MODEL_ID ));
		}
		
		/**
		* return a reference to MediaModel
		*/
		
		protected function getMediaModel() : MediaModel
		{
			return MediaModel ( ModelLocator.getInstance().getModel( MediaController.MEDIA_MODEL_ID ));
		}
		
		/**
		* return a reference to WiiavieMainView
		*/
		
		public function getCurrentView( ) : WiiavieMainView
		{
			return WiiavieMainView( getView() );
		}
		
		/**
		* return a reference to WiiavieVisioWidgetManager
		*/
		
		protected function getVisioWidgetManager( ) : WiiavieVisioWidgetManager
		{
			return WiiavieVisioWidgetManager( getCurrentView().visioView.manager );
		}
		
		/**
		* return a reference to WiiavieMediaWidgetManager
		*/
		
		protected function getMediaWidgetManager( ) : WiiavieMediaWidgetManager
		{
			return WiiavieMediaWidgetManager( getCurrentView().mediaView.manager );
		}
	}
}